﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using TestClient.SocialLearning;
using SocialLearning;
using System.IO;

namespace TestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            TestClient.SocialLearning.SocialLearningServiceClient server =
                new TestClient.SocialLearning.SocialLearningServiceClient();

            PingListener listen = new PingListener(new Random().Next(1025, 65535));
            listen.PingReceived += new PingHandler(ListenPingReceived);

            byte[] session = server.Login("guest", "password");
            server.SetClientPingEndpoint(session, "127.0.0.1", listen.Port);
            listen.StartListen();

            Console.WriteLine("Modules:");
            string[] mods = server.GetModuleList(session);
            if (mods != null)
            {
                foreach (string s in mods) { Console.WriteLine(" > {0}", s); }
            }
            Console.WriteLine("---");

            Console.WriteLine("Methods in {0}:", "Test");
            string[] meths = server.GetMethodList(session, "Test");
            if (meths != null)
            {
                foreach (string s in meths) { Console.WriteLine(" > {0}", s); }
            }
            Console.WriteLine("---");
            Console.WriteLine("");

            try
            {
                Console.WriteLine("Test:XYZ();");
                Result x = server.InvokeMethod(session, "Test", "XYZ", null) as Result;
                if (x == null) { Console.WriteLine("null"); }
                else if (x.Error != null) { Console.WriteLine("Error: {0}", x.Error); }
                else { Console.WriteLine("{0}", x.ReturnValue); }
            }
            catch (Exception x) { Console.WriteLine(x.Message); }
            Console.WriteLine();
            try
            {
                Console.WriteLine("Test:XXX();");
                Result x = server.InvokeMethod(session, "Test", "XXX", null);
                if (x == null) { Console.WriteLine("null"); }
                else if (x.Error != null) { Console.WriteLine("Error: {0}", x.Error); }
                else
                {
                    Console.WriteLine("{0}", x.ReturnValue);
                }
            }
            catch (Exception x) { Console.WriteLine(x.Message); }
            Console.WriteLine();
            try
            {
                Console.WriteLine("Test:GetFeed();");
                Result x = server.InvokeMethod(session, "Test", "GetFeed", null);
                if (x == null) { Console.WriteLine("null"); }
                else if (x.Error != null) { Console.WriteLine("Error: {0}", x.Error); }
                else
                {
                    Console.WriteLine("{0}", x.ReturnValue);
                }
            }
            catch (Exception x) { Console.WriteLine(x.Message); }
            Console.WriteLine();
            try
            {
                Console.WriteLine("Test:GetFeeds();");
                Result x = server.InvokeMethod(session, "Test", "GetFeeds", null);
                if (x == null) { Console.WriteLine("null"); }
                else if (x.Error != null) { Console.WriteLine("Error: {0}", x.Error); }
                else
                {
                    Console.WriteLine("{0}", x.ReturnValue);
                }
            }
            catch (Exception x) { Console.WriteLine(x.Message); }
            Console.WriteLine();
            try
            {
                Console.WriteLine("Test:GetX();");
                Result x = server.InvokeMethod(session, "Test", "GetX", null);
                if (x == null) { Console.WriteLine("null"); }
                else if (x.Error != null) { Console.WriteLine("Error: {0}", x.Error); }
                else
                {
                    Console.WriteLine("{0}", x.ReturnValue);
                    X y = x.ReturnValue as X;
                    Console.WriteLine(y.Y);
                    Console.WriteLine(y.Y.Length);
                    Console.WriteLine(y.Z);
                }
            }
            catch (Exception x) { Console.WriteLine(x.Message); }
            Console.WriteLine();
            try
            {
                Console.WriteLine("Test:EchoFeed();");
                AMP_Feeds feeds = new AMP_Feeds();
                feeds.Feeds = new AMP_Feed[] { new AMP_Feed(), new AMP_Feed(), new AMP_Feed() };

                Result x = server.InvokeMethod(session, "Test", "EchoFeed",
                    new object[]
                    {
                        "p1",
                        feeds,
                        "p2"
                    });

                if (x == null) { Console.WriteLine("null"); }
                else if (x.Error != null) { Console.WriteLine("Error: {0}", x.Error); }
                else
                {
                    Console.WriteLine("{0}", x.ReturnValue);
                }
            }
            catch (Exception x) { Console.WriteLine(x.Message); }
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("Type some text: ");
            server.InvokeMethod(session, "Test", "Echo", new object[] { Console.ReadLine() });

            Console.ReadLine();

            listen.StopListen();
            server.Logout(session);
        }

        static void ListenPingReceived(int pingType, string pingData)
        {
            Console.WriteLine("<Pinged:{0}> {1}", pingType, pingData);
        }
    }
}
